package com.niiwoo.civet.mobile.service.bill.detail;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.dto.response.NiiwooBillRespDTO;
import com.niiwoo.civet.account.dto.response.UserBillBaseResponseDTO;
import com.niiwoo.civet.mobile.service.bill.UserBillInvestOrderService;
import com.niiwoo.civet.mobile.vo.response.bill.JumpTypeEnum;
import com.niiwoo.civet.mobile.vo.response.bill.UserBillDetailResponse;
import com.niiwoo.civet.trade.dto.response.ProjectDetailDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.ProjectPackageInvestOrderDTO;
import com.niiwoo.civet.trade.service.intelligenceInvest.IntelligenceOrderDubboService;
import com.niiwoo.civet.trade.service.project.ProjectDubboService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 回款账单说情
 * Created by Yijian on 2018/11/8.
 */
@Slf4j
@Service
public class RecerivedBillDetailStrategyImpl implements BillDetailStrategy {

    @Reference(version = "1.0.0")
    private ProjectDubboService projectDubboService;
    @Reference(version = "1.0.0")
    private IntelligenceOrderDubboService intelligenceOrderDubboService;
    @Autowired
    private UserBillInvestOrderService projectDetailService;


    @Override
    public String build(UserBillBaseResponseDTO userBillBase, List<NiiwooBillRespDTO> userBillFlowList, UserBillDetailResponse response) {
        //回款账单详情页面上的订单编号显示为“投资订单id”
        if(Objects.nonNull(userBillBase.getInvestId())){
            response.setOrderId(userBillBase.getInvestId().toString());
        }

        // 查询标的
        ProjectDetailDTO projectDetailDTO = projectDubboService.getProjectById(Long.valueOf(userBillBase.getProjectId()));
        log.info("=================projectDubboService.getProjectById: deadlineUnit:{},deadline:{},borrowRate:{},activityRaiseRate:{},nickName:{},headImg:{}",
                projectDetailDTO.getDeadlineUnit(),projectDetailDTO.getDeadline(),projectDetailDTO.getBorrowRate(),projectDetailDTO.getActivityRaiseRate(),projectDetailDTO.getBorrowNickName(),projectDetailDTO.getBorrowHeadImage());

        //组装回款说明
        UserBillDetailResponse.SpecialItem specialItem = response.new SpecialItem();
        specialItem.setName("回款说明");
        StringBuilder content = new StringBuilder();
        // 判断为智能投，返回借款人头像昵称
        if (!userBillBase.getProjectId().equals(userBillBase.getProjectPackageId())) {
            specialItem.setUserType(projectDetailDTO.getUserType());
            //智能投
            if (StringUtils.isNotBlank(projectDetailDTO.getBorrowHeadImage())) {
                specialItem.setImgUrl(projectDetailDTO.getBorrowHeadImage());
            } else {
                specialItem.setImgUrl("");
            }
            if (StringUtils.isNotBlank(projectDetailDTO.getBorrowNickName())) {
                content.append(projectDetailDTO.getBorrowNickName()).append(" ");
            }
        }
        content.append(userBillBase.getCurrentPeriod() + "/" + userBillBase.getTotalPeriods() + "期");
        specialItem.setContent(content.toString());
        if(userBillBase.getInvestId() > 0){
            specialItem.setIsNeedJump(Boolean.TRUE);
        }else {
            specialItem.setIsNeedJump(Boolean.FALSE);
        }
        specialItem.setJumpType(JumpTypeEnum.RECERIVED_PLAN.getCode());
        specialItem.setJumpParamOne(userBillBase.getInvestId().toString());//投资订单id
        response.getSpecialItems().add(specialItem);

        //组装投资订单
        String investOrderContent = projectDetailService.getInvestOrderContent(userBillBase,response);
        if(!userBillBase.getProjectId().equals(userBillBase.getProjectPackageId())){
            ProjectPackageInvestOrderDTO investOrderDTO = intelligenceOrderDubboService.getProjectPackageInvestOrderBySubInvestOrderId(userBillBase.getInvestId());
            if(Objects.nonNull(investOrderDTO) && investOrderDTO.getId() > 0){
                specialItem = response.new SpecialItem();
                specialItem.setName("投资订单");
                specialItem.setContent(investOrderContent);
                if(investOrderDTO.getId() > 0){
                    specialItem.setIsNeedJump(Boolean.TRUE);
                }else {
                    specialItem.setIsNeedJump(Boolean.FALSE);
                }
                specialItem.setIsNeedJump(Boolean.TRUE);
                specialItem.setJumpType(JumpTypeEnum.ZNT_INVEST_ORDER.getCode());
                specialItem.setJumpParamOne(investOrderDTO.getId().toString());
                response.getSpecialItems().add(specialItem);
            }
        }else {
            specialItem = response.new SpecialItem();
            specialItem.setName("投资订单");
            specialItem.setContent(investOrderContent);
            if(userBillBase.getInvestId() > 0){
                specialItem.setIsNeedJump(Boolean.TRUE);
            }else {
                specialItem.setIsNeedJump(Boolean.FALSE);
            }
            specialItem.setJumpType(JumpTypeEnum.INVEST_ORDER.getCode());
            specialItem.setJumpParamOne(userBillBase.getInvestId().toString());
            response.getSpecialItems().add(specialItem);
        }

        specialItem = response.new SpecialItem();
        specialItem.setName("订单编号");
        specialItem.setContent(response.getOrderId());
        response.getSpecialItems().add(specialItem);

        return JSON.toJSONString(response);
    }
}
